package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.RoomBean;
import org.yscz.entity.vo.RecordVo;
import org.yscz.service.EvaluationService;

import java.util.UUID;

/**
 * @author achen
 * @create 2021-08-19 13:37
 */
@RestController
@Slf4j
@Api("测评接口")
public class EvaluationController {

    @Value("${yscz.usbDevice.cameraUrl}")
    private String cameraUrl;

    @Autowired
    private EvaluationService evaluationService;

    @Value("${yscz.mediaServer.ip}")
    private String mediaIp;

    @Value("${yscz.mediaServer.flv-port}")
    private Integer flvPort;


    @ApiOperation(value = "开始测评", httpMethod = "POST")
    @PostMapping("/evaluation/startEvaluation")
    public Result<JSONObject> startEvaluation() {
        String uuid = UUID.randomUUID().toString();

        RoomBean roomBean = new RoomBean();
        roomBean.setUuid(uuid);
        roomBean.setSsUrl(uuid);
        roomBean.setUserId("111");
        roomBean.setStreamWay(1);
        roomBean.setCameraUrl(cameraUrl);
        roomBean.setAudioUrl(cameraUrl);

        //开始分析
        evaluationService.startEvaluation(roomBean, true);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uuid", uuid);
        //rtsp流转换为.flv
        jsonObject.put("cameraUrl", FaceController.flvMethod(uuid, mediaIp, flvPort));

        return Result.success(HttpStatus.SUCCESS, "视频开启成功", jsonObject);
    }

    @ApiOperation(value = "结束测评", httpMethod = "POST")
    @PostMapping("/evaluation/endEvaluation")
    public Result endEvaluation(@RequestBody RecordVo recordVo) throws InterruptedException {
        RoomBean roomBean = new RoomBean();
        roomBean.setUuid(recordVo.getRecordUuid());
        evaluationService.endEvaluation(roomBean);

        return Result.success(HttpStatus.SUCCESS, "视频关闭成功", null);
    }

    @ApiOperation(value = "获取被测人测评信息", httpMethod = "GET")
    @GetMapping("/evaluation/getEvalInfos")
    public Result getEvalInfos(@RequestParam(value = "testeeId") Long testeeId,
                               @RequestParam(value = "scaleId") Long scaleId,
                               @RequestParam(value = "recordId", required = false) Long recordId) {

        if (testeeId == null && scaleId == null) {
            return Result.error(HttpStatus.BAD_REQUEST, "参数列表错误（缺少，格式不匹配）");
        }

        JSONObject dataObj = new JSONObject();
        dataObj.put("testeeId", testeeId);
        dataObj.put("scaleId", scaleId);

        if (recordId != null) {
            dataObj.put("recordId", recordId);
        }

        return Result.success(HttpStatus.SUCCESS, "获取被测人测评信息成功", evaluationService.getEvalInfos(dataObj));
    }
}
